#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

struct ListNode {
    int val;
    struct ListNode* next;
};

struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) {
    int len1, len2;
    struct ListNode* n1, * n2;
    len1 = len2 = 0;
    n1 = headA, n2 = headB;
    while (n1)
    {
        len1++;
        n1 = n1->next;
    }
    while (n2)
    {
        len2++;
        n2 = n2->next;
    }
    int a = abs(len1 - len2);
    n1 = headA, n2 = headB;
    if (len1 > len2)
    {
        while (a)
        {
            n1 = n1->next;
            a--;
        }
    }
    if (len1 < len2)
    {
        while (a)
        {
            n2 = n2->next;
            a--;
        }
    }
    while (n1 && n2)
    {
        if (n1 == n2)
        {
            return n1;
        }
        n1 = n1->next;
        n2 = n2->next;

    }
    return NULL;
}